package javaCode.chapter07.poly_.polyarr_;

public class PolyArray {
    public static void main(String[] args) {
        //应用实例：现有一个继承结构如下：要求创建1个Person对象、
        // 2个Student 对象和2个Teacher对象，统一放在数组中，并调用每个对象say方法

        Person[] persons = new Person[5];
        persons[0] = new Person("Nick", 30);
        persons[1] = new Student("jack",19,100);
        persons[2] = new Student("smith",18,50);
        persons[3] = new Teacher("black",28,5000);
        persons[4] = new Teacher("Ms.wang",30,7000);

        //循环遍历输出say方法
        for (int i = 0; i < persons.length; i++) {
            //提示：person[i] 编译类型是 Person ，运行类型是是根据实际情况由JVM来判断
            System.out.println(persons[i].say()); //动态绑定机制

            //使用了类型判断 + 向下转型
            if (persons[i] instanceof Student){
                Student student = (Student) persons[i];
                student.study();
                //也可以用一条语句 ((Student)persons[i]).study();
            }else if (persons[i] instanceof Teacher){
                ((Teacher)persons[i]).teach(); //一条语句
            }
        }

    }
}
